<template>
	<view class="main">
		<web-view :src="url"></web-view>
	</view>

</template>

<script>
	import { $payOpenId, $placeOpenid, $fansPay, $placeCode } from '@/http/apiManager.js'
	import storageManage from '@/util/storageManage.js'
	import appConfig from '@/config/appConfig.js'
	export default {
		data() {
			return {
				isShow: true,
				url: '',
				ary: [{
					url: appConfig.url, //支付公众号openid ;loading页面
					// url: 'https://tzyzwjr.498.net/h5/loginCodeZZ.html?r=https://pay.jdyd.xyz/openid_498.html' //498获取公众号方式openid；完善个人信息页面
				}],
				code: [],
			}
		},
		onReady() {
			uni.hideHomeButton()
		},
		onLoad(options) {
			if (options.code) {
				this.code.push(options.code)
			}
			if (this.code.length == this.ary.length) {
				if (options.type == 'placeCode'){	// 通过code换取地域公众号openid
					// 拿到保存的临时变量
					const officialAppId = getApp().globalData.temp_official_appid
					const path = getApp().globalData.temp_path
					$placeCode({ officialAppId, code: options.code }).then(({ bizData }) => {
						/**
						 * 公众号授权 涉及历史福分同步（郑州）以及公众号跳转活动根据公众号openid判断是否是转发消息 ；所以保存的时候要加以区分(appid)storageManage.placeOpenId
						 */
						const { appId, openId } = bizData
						storageManage.placeOpenId(appId,openId)	// 保存公众号授权openId 以appId作为key值
						// 判断是否是同主体发券
						const payAuthUrl = getApp().globalData.temp_pay_auth_url
						const payAuthIfCode = getApp().globalData.temp_ifcode
						if(payAuthUrl && payAuthIfCode){
							// 删除临时变量授权地址（支付通道标识在支付授权后再进行删除）
							delete getApp().globalData.temp_pay_auth_url
							delete getApp().globalData.temp_official_appid
							appConfig.url = payAuthUrl
							uni.redirectTo({
								url: '/pages/openid/openid',
							})
							return
						}
						// 清除临时变量
						delete getApp().globalData.temp_official_appid
						delete getApp().globalData.temp_path
						// 跳转页面 直接跳转活动页面
						uni.redirectTo({
							url: path
						})
					})
				} else if (options.type == 'pay'){
					// 三方出资
					const path = getApp().globalData.temp_path
					const ifCode = getApp().globalData.temp_ifcode
					// const { joinType } = parseUrlParams(path)
					// 清除临时变量
					delete getApp().globalData.temp_ifcode
					delete getApp().globalData.temp_path
					$fansPay({ ifCode, openId: options.code }).then(res => {
						storageManage.openId(`${ifCode}-openId`,options.code)
						if(path.includes('type=tabbar')){	// 跳转tabbar页不能使用uni.redirectTo
							uni.reLaunch({
								url: path
							})
						}else{
							uni.redirectTo({
								url: path
							})
						}
					})
					
				} else {
					//通过code换取openid----支付公众号
					const path = getApp().globalData.temp_path
					const ifCode = getApp().globalData.temp_ifcode
					// 清除临时变量
					delete getApp().globalData.temp_ifcode
					delete getApp().globalData.temp_path
					var json = {
						code: this.code[0]
					}
					// var jsonStr = JSON.stringify(json)
					$payOpenId(json).then(({ bizData }) => {
						storageManage.fansId(bizData.fansId)
						storageManage.payOpenId(bizData.payOpenId)
						uni.reLaunch({
							url: `/pages/welcome/index`
						})
					})
				}

			} else {
				this.url = this.ary[this.code.length].url
			}
		},
		mounted() {

		},
		methods: {

		}
	}
	
	/** 地址参数解析 */
	function parseUrlParams(url) {
	    var params = {};
	    var paramsString = url.split('?')[1];
	
	    if (paramsString) {
	        var paramsArray = paramsString.split('&');
	        
	        paramsArray.forEach(function(param) {
	            var keyValue = param.split('=');
	            var key = keyValue[0];
	            var value = keyValue[1];
	            params[key] = value;
	        });
	    }
	    return params;
	}
</script>

<style lang="scss" scoped>

</style>